<?php
if (!defined("__WD__")) exit("Wrong Connect");

class Member extends __Controller {

	function __construct() {
		parent::__construct();

		$this->load->config("member", $this->config);

		$this->load->module( 'input' , $this->input );
		$this->load->model( 'admin/admin' , $this->admin );
		$this->load->model( 'admin/member' , $this->member );

		setHeader("html", "utf-8");
	}

	function login() {
		
		$ret = $this->member->login($this->input->post('id'), $this->input->post('password'));
		
		if (!$ret)
			alert("아이디 또는 비밀번호가 잘못되었습니다.");
		
		go( $this->input->post('go') );
			
		exit;
		
	}
	function join() {

		$this->load->module("mysql", $this->db);
		
		$id			= $this->input->post("id");
		$email		= $this->input->post("email");

		$password1	= $this->input->post("password1");
		$password2	= $this->input->post("password2");
		
		$nick		= $this->input->post("nick");
		
		$options = $this->input->post("options");
		
		if ( isset($options) && $options !== "" ) {
			$options		= array();
			$options_list	= explode(",", $this->input->post("options"));
			foreach ($options_list as $v) { $options[$v] = $this->input->post($v); }
			$options	= serialize($options);
		}
		
		if ( $this->db->count("select count(*) from `wd_member` where `id`=?", array($id)) > 0 ) alert("이미 같은 아이디가 존재합니다.");
		if ( !preg_match('/^[^\W][a-zA-Z0-9_-]+(\.[a-zA-Z0-9_]+)*\@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)*\.[a-zA-Z]{2,4}$/', $email) ) alert("잘못된 메일주소 입니다.");
		if ( $this->db->count("select count(*) from `wd_member` where `email`=?", array($email)) > 0 ) alert("이미 같은 메일주소가 존재합니다.");
		if ( $password1 !== $password2 ) alert("입력한 두 비밀번호가 다릅니다.");
		
		$dat = array(
			'id'		=> $id,
			'email'		=> $email,
			'password'	=> $this->admin->password($password1),
			'nick'		=> $nick,
			'options'	=> $options
		);

		if ($this->config['form']['jumin']) {
			
			$birth = $this->input->post("birth");
			$jumin = $this->input->post("jumin");
			
			if ( !preg_match('/([0-9])([0-9])([0-1])([0-9])([0-3])([0-9])/', $birth) ) alert("잘못된 주민번호 입니다.1");
			if ( !preg_match('/([0-3])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])/', $jumin) ) alert("잘못된 주민번호 입니다.2");
			
			$dat['birth'] = $birth;
		}
		if ($this->config['form']['phone']) {
			
			$phone = $this->input->post("phone0")."-".$this->input->post("phone1")."-".$this->input->post("phone2");
			if ( !preg_match('/^(01[016789]{1}|02|0[3-9]{1}[0-9]{1})-?[0-9]{3,4}-?[0-9]{4}$/', $phone) ) alert("잘못된 연락처입니다.");
			
			$dat['phone'] = $phone;
		}

		if ( $this->db->insert("wd_member", $dat) === false )
			alert("DB를 쓰는 도중 실패하였습니다.");
		
		alert("회원가입이 완료되었습니다. 로그인해주세요.", $this->input->post('go'));
		go( $this->input->post('go') );
		
		exit;
		
	}
	function modify() {

		$this->load->module("mysql", $this->db);
		
		$idx	= $this->input->post("idx");
		
		$origin = $this->db->result("select * from `wd_member` where `idx`=?", array($idx) );
		$origin['options'] = @unserialize($origin['options']);
		
		$email		= $this->input->post("email");

		$password1	= $this->input->post("password1");
		$password2	= $this->input->post("password2");
		
		$nick		= $this->input->post("nick");
		
		$options		= $origin['options'];
		$options_list	= explode(",", $this->input->post("options"));
		foreach ($options_list as $v) { $options[$v] = $this->input->post($v); }
		$options	= serialize($options);

//		if ( $this->db->count("select count(*) from `wd_member` where `id`=?", array($id)) > 0 ) alert("이미 같은 아이디가 존재합니다.");
		if ( !preg_match('/^[^\W][a-zA-Z0-9_-]+(\.[a-zA-Z0-9_]+)*\@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)*\.[a-zA-Z]{2,4}$/', $email) ) alert("잘못된 메일주소 입니다.");
		if ( $this->db->count("select count(*) from `wd_member` where `email`=? and `idx`!=?", array($email, $idx)) > 0 ) alert("이미 같은 메일주소가 존재합니다.");
		if ( $password1 !== $password2 ) alert("입력한 두 비밀번호가 다릅니다.");
		
		$dat = array(
			'email'		=> $email,
			'nick'		=> $nick,
			'options'	=> $options
		);

		if ($this->config['form']['phone']) {
			
			$phone = $this->input->post("phone0")."-".$this->input->post("phone1")."-".$this->input->post("phone2");
			if ( !preg_match('/^(01[016789]{1}|02|0[3-9]{1}[0-9]{1})-?[0-9]{3,4}-?[0-9]{4}$/', $phone) ) alert("잘못된 연락처입니다.");
			
			$dat['phone'] = $phone;
		}


		if ($password1 != "")	$dat['password'] = $this->admin->password($password1);
		
		if ( $this->db->update("wd_member", $dat, array('idx' => $idx)) === false )
			alert("DB를 쓰는 도중 실패하였습니다.");
		
		go( $this->input->post('go') );
		
		exit;
		
	}
	function findid() {

		$this->load->module("mysql", $this->db);
		
		$nick	= $this->input->post("nick");
		$email	= $this->input->post("email");
		
		$result = $this->db->fetch_assoc("select `id` from `wd_member` where `nick`=? and `email`=?", array($nick, $email));
		
		if ( count($result) == 0 ) alert("해당하는 회원이 존재하지 않습니다.");
		
		$id = $result[0]['id'];

		$contents = $this->_makeContents( "회원님께서 문의하신 아이디는 <strong>{$id}</strong>입니다." );

		$this->load->helper("mail");
		$this->load->config("member", $config);

		mailto("초앤유 여성병원", $config['admin']['email'], $email, "[초앤유여성병원] 회원님께서 문의하신 아이디입니다.", $contents); // $html = "0(1:사용, 0:사용안함)";

		alert("문의하신 아이디는 메일로 발송하였습니다.");
		exit;
	}

	function findpw() {

		$this->load->module("mysql", $this->db);
		
		$id		= $this->input->post("id");
		$nick	= $this->input->post("nick");
		$email	= $this->input->post("email");
		
		$result = @$this->db->fetch_assoc("select `idx` from `wd_member` where `id`=? and `nick`=? and `email`=?", array($id, $nick, $email));
		
		if ( count($result) == 0 ) alert("해당하는 회원이 존재하지 않습니다.");
		
		$idx = $result[0]['idx'];
		$pw = rand(1000,9999).chr(rand(97,122)).chr(rand(97,122)).chr(rand(97,122)).chr(rand(97,122));
		
		$this->db->update("wd_member", array( 'password' => $this->admin->password($pw) ), array('idx' => $idx) );
		
		$contents = $this->_makeContents( "{$id} 님의 임시비밀번호는 <strong>{$pw}</strong>입니다." );

		$this->load->helper("mail");
		$this->load->config("member", $config);
				
		mailto("초앤유 여성병원", $config['admin']['email'], $email, "[초앤유여성병원] 회원님께서 문의하신 임시비밀번호입니다.", $contents);

		alert("임시비밀번호를 메일로 발송하였습니다.");
		exit;
	}
	function leave() {

		$this->load->module("mysql", $this->db);
		
		$idx		= $this->input->post("idx");
		$id			= $this->input->post("id");
		$password	= $this->input->post("password");
		
		$origin = $this->db->result("select * from `wd_member` where `idx`=? and `id`=? and `password`=?", array($idx, $id, $this->admin->password($password)) );
		
		if ( !isset($origin) || $origin == "" || $origin == null ) alert("아이디랑 비밀번호를 잘못 입력하셨습니다.");
		
		if ( $this->db->delete("wd_member", array('idx' => $idx)) === false )
			alert("DB를 쓰는 도중 실패하였습니다.");

		$this->member->logout();

		alert( "이용해주셔서 감사합니다.", $this->input->post('go') );
		
		exit;
	}
	
	function test() {
		echo $this->_makeContents( "contents" );
	}
	
	
	function _makeContents($contents) {
		$hd = "http://".HOME."/library/grp/cho/img/mail/mail_header.png";
		$ft = "http://".HOME."/library/grp/cho/img/mail/mail_footer.png";
		return <<<EOT
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<style>
html,body {margin:0;padding:0;}
</style>
<title>비밀번호찾기</title>

</head>
<body>
<div class="container" style="width:518px;height:300px;margin:0 auto;">
	<div class="hd" style="height:79px;width:518px;background:url('{$hd}')"></div>
	<div class="con" style="height:182px;background:#ebebeb;width:518px;">
		<div style="font-size:12px;color:#000;text-align:center;padding-top:70px;line-height:20px;">
			{$contents}<br />
			감사합니다.
		</div>
	</div>
	<div class="ft" style="height:39px;width:518px;"><a href="http://chowoman.com" target="_blank"><img src="{$ft}" alt="" /></a></div>
</div>
</body>
</html>
EOT;
	}
}
